package cn.xspace.library.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.base.BaseActivity;

import java.io.File;

import cn.xspace.library.R;
import cn.xspace.library.view.TouchImageView;

/**
 * 图片预览界面
 */
public class OSCPhotosActivity extends BaseActivity {

    public static final String BUNDLE_KEY_IMAGES = "bundle_key_images";
    private TouchImageView mTouchImageView;
    private ProgressBar mProgressBar;
    //    private ImageView mOption;
    private String mImageUrl;

    public static void showImagePreview(Context context,
                                        String imageUrl) {
        Intent intent = new Intent(context, OSCPhotosActivity.class);
        intent.putExtra(BUNDLE_KEY_IMAGES, imageUrl);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        super.onCreate(savedInstanceState);

    }


    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        mTouchImageView = (TouchImageView) findViewById(R.id.photoview);

        mTouchImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mProgressBar = (ProgressBar) findViewById(R.id.pb_loading);
        loadImage(mTouchImageView, mImageUrl);

        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }

    private void showOptionMenu() {

    }


    private String getFileName(String imgUrl) {
        int index = imgUrl.lastIndexOf('/') + 1;
        if (index == -1) {
            return System.currentTimeMillis() + ".jpeg";
        }
        return imgUrl.substring(index);
    }

    /**
     * Load the item's thumbnail image into our {@link ImageView}.
     */
    private void loadImage(final ImageView mHeaderImageView, final String imageUrl) {

        if (TextUtils.isEmpty(imageUrl)) {
            mHeaderImageView.setImageResource(R.drawable.chat_default_image_error);
        } else {
            File mFile = new File(imageUrl);
            if (mFile.exists()) {
                //加载本地的图片
                Picasso.with(mContext)
                        .load(mFile)
                        .placeholder(R.drawable.chat_default_image)
                        .error(R.drawable.chat_default_image_error)
                        .into(mHeaderImageView, new Callback() {
                            @Override
                            public void onSuccess() {
                                mProgressBar.setVisibility(View.GONE);
                                mHeaderImageView.setVisibility(View.VISIBLE);
//                            mOption.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onError() {

                                mProgressBar.setVisibility(View.GONE);
                                mHeaderImageView.setVisibility(View.VISIBLE);
                            }
                        });
            } else {
                Picasso.with(mContext)
                        .load(imageUrl)
                        .placeholder(R.drawable.chat_default_image)
                        .error(R.drawable.chat_default_image_error)
                        .into(mHeaderImageView, new Callback() {
                            @Override
                            public void onSuccess() {
                                mProgressBar.setVisibility(View.GONE);
                                mHeaderImageView.setVisibility(View.VISIBLE);
//                            mOption.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onError() {
                                mProgressBar.setVisibility(View.GONE);
                                mHeaderImageView.setVisibility(View.VISIBLE);
                            }
                        });
            }


        }
        mProgressBar.setVisibility(View.GONE);
//        Glide.with(this).load(imageUrl).asBitmap().into(new SimpleTarget<Bitmap>() {
//            @Override
//            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                mHeaderImageView.setImageBitmap(resource);
//                mProgressBar.setVisibility(View.GONE);
//                mHeaderImageView.setVisibility(View.VISIBLE);
//                mOption.setVisibility(View.VISIBLE);
//            }
//        });
    }

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_photo_browse);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    @Override
    protected void getIntentData() {
        super.getIntentData();
        mImageUrl = getIntent().getStringExtra(BUNDLE_KEY_IMAGES);
    }
}
